'use strict';

const Controller = require('egg').Controller;
let jwt = require("jsonwebtoken")

class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = 'hi, egg';
  }

  //移动登录
  async login() {
    const { app, ctx } = this
    const { username, password } = ctx.request.body
    const data = await app.mysql.query(`select * from login where username = "${username}" and password = "${password}"`)

    //遍历登录成功的数据存入到datas
    const datas = data.map(item => {
      return {
        url: item.url,
        name: item.name,
      }
    })

    if (data.length) {
      //登陆成功传入datas
      ctx.body = {
        code: 1,
        datas,
        token: jwt.sign({ ...data }, '1901B', { expiresIn: '3m' }),
      }
    } else {
      ctx.body = {
        code: 0,
      }
    }
  }

  //渲染科普数据
  async learnList() {
    const { app, ctx } = this
    const data = await app.mysql.select('learn')

    ctx.body = {
      code: 1,
      data
    }
  }

  //渲染科普热点数据
  async learnhotList() {
    const { app, ctx } = this
    const data = await app.mysql.select('learnHot')

    ctx.body = {
      code: 1,
      data
    }
  }

  //科普搜索
  async learnCheck() {
    const { app, ctx } = this
    const { value } = ctx.request.body
    const data = await app.mysql.query(`select * from learnHot where title like "%${ value }%"`)

    ctx.body = {
      code: 1,
      data
    }
  }

  //渲染商城数据
  async shopList() {
    const { app, ctx } = this
    const data = await app.mysql.select('shop')

    ctx.body = {
      code: 1,
      data
    }
  }

  //渲染购物车数据
  async cart() {
    const { app, ctx } = this
    const data = await app.mysql.select('cart')

    ctx.body = {
      code: 1,
      data
    }
  }

  // //购物车数据全选
  // async carIs() {
  //   const { ctx, app } = this;
  //   const { id, is } = ctx.request.body
  //   const data = await app.mysql.update('cart', { id, is })

  //   ctx.body = {
  //     code: 1,
  //     data
  //   }
  // }

  //购物车列表删除
  async cartDel() {
    const { ctx, app } = this;
    const { value } = ctx.request.body
    const data = await app.mysql.delete('cart', { id:value })

    ctx.body = {
      code: 1,
      data
    }
  }

  //商城加入购物车
  async addCart() {
    const { ctx, app } = this;
    const { drugimg,price,drug,id } = ctx.request.body.info
    const data = await app.mysql.insert('cart', { id,drugimg,price,drug,num:1 })
    console.log(data);
  
    ctx.body = {
      code: 1,
      data
    }
  }

  //PC端登录
  async PClogin() {
    const { app, ctx } = this
    const { username, password } = ctx.request.body
    console.log(username);
    
    const data = await app.mysql.query(`select * from login where username = "${username}" and password = "${password}"`)

    //遍历登录成功的数据存入到datas
    const datas = data.map(item => {
      return {
        url: item.url,
        name: item.name,
        type: item.type
      }
    })

    if (data.length) {
      //登陆成功传入datas
      ctx.body = {
        code: 1,
        datas,
        token: jwt.sign({ ...data }, '1901B', { expiresIn: '3m' }),
      }
    } else {
      ctx.body = {
        code: 0,
      }
    }
  }

  //PC渲染科普热点数据
  async PChotList() {
    const { app, ctx } = this
    const data = await app.mysql.select('learnHot')
    const { authorization } = ctx.request.header

    try {
      jwt.verify(authorization, '1901B')
      ctx.body = {
        code: 1,
        msg: "成功",
        data
      }
    } catch (error) {
      ctx.body = {
        code: 0,
        msg: '登录失效，请重新登录'
      }
    }
  }

  //PC热点新增数据
  async PCaddhot() {
    const { ctx, app } = this;
    const { title,num,time,type } = ctx.request.body
    const data = await app.mysql.insert('learnHot', { title,num,time,type,
      img:'https://staff.chunyu.mobi/@/media/images/2021/09/30/9f3a/0c1db3e5064a_w600_h450_.jpg?imageView2/1/w/120/h/116',
    number:'80' })
  
    ctx.body = {
      code: 1,
      data
    }
  }

  //PC热点删除数据
  async PCdelhot() {
    const { ctx, app } = this;
    const { id } = ctx.request.body
    const data = await app.mysql.delete('learnHot', { id })
  
    ctx.body = {
      code: 1,
      data
    }
  }

  //PC热点修改数据
  async PCupdatehot() {
    const { ctx, app } = this;
    const { title,num,time,type,id } = ctx.request.body
    const data = await app.mysql.update('learnHot', { id,title,num,time,type })
  
    ctx.body = {
      code: 1,
      data
    }
  }

  //PC热点查找数据
  async PCcheckhot() {
    const { ctx, app } = this;
    const { value } = ctx.request.body
    const data = await app.mysql.query(`select * from learnHot where title like "%${ value }%"`)
  
    ctx.body = {
      code: 1,
      data
    }
  }
}

module.exports = HomeController;
